/*
 * BanBu_ProfileOp
 * @description 本文件提供更换个人空间背景图片的方法
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供更换个人空间背景图片的方法
 */

#import "BanBu_ProfileOp.h"
#import "AppCommunicationManager.h"

@implementation BanBu_ProfileOp

static BanBu_ProfileOp *operation = nil;

+(BanBu_ProfileOp *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        operation = [[self alloc]init];
        
    });
    
    return operation;
}

- (void)setWaveImageForViewController:(UIViewController *)_viewcontroller
{
    targetViewController = _viewcontroller;
    UIActionSheet *choose = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"chooseWaveImage", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"funLabel", nil),NSLocalizedString(@"funLabel1", nil), nil];
    [choose showInView:_viewcontroller.view];
    
    [choose release];
}
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        }
        [picker setAllowsEditing:YES];
        [targetViewController presentViewController:picker animated:YES];
        [picker release];
    }
    else if(buttonIndex == 1)
    {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [picker setAllowsEditing:YES];
        [targetViewController presentViewController:picker animated:YES];
        [picker release];
    }
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //    NSLog(@"%@",info);
    //    //    UIImagePickerControllerOriginalImage
    //    uploadImage = [info valueForKey:UIImagePickerControllerEditedImage];
    //    [avatarButton setImage:uploadImage forState:UIControlStateNormal];
    //    isReadyNext = YES;
    //    [picker dismissViewControllerAnimated:YES];
    [picker dismissViewControllerAnimated:YES];

     UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
    
//    NSMutableDictionary *sendDic = [NSMutableDictionary dictionaryWithCapacity:1];
//    [sendDic setValue:@"jpg" forKey:@"extname"];
//    [AppComManager uploadBanBuBroadcastMedia:UIImageJPEGRepresentation(image, 0.7) mediaName:@"tupian" par:sendDic delegate:self];
//    [TKLoadingView showTkloadingAddedTo:targetViewController.view title:NSLocalizedString(@"uploading", nil) activityAnimated:YES];
    
    
    NSData *data = nil;
    data = UIImageJPEGRepresentation(image, 0.7);
    NSMutableDictionary *uploadDic = [NSMutableDictionary dictionary];
    [uploadDic setValue:@"y" forKey:@"phone"];
    [uploadDic setValue:@"jpg" forKey:@"picformat"];
    [AppComManager uploadRegAvatarImage:data Par:uploadDic delegate:self];
    
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES];
}

#pragma mark - BanBuUploadRequsetDelegate

- (void)banbuUploadRequest:(NSDictionary *)resDic didFinishedWithError:(NSError *)error{
    [TKLoadingView dismissTkFromView:targetViewController.view animated:NO afterShow:0.0];
    if(error){
        [TKLoadingView showTkloadingAddedTo:targetViewController.view title:NSLocalizedString(@"uploadFailNotice", nil) activityAnimated:NO duration:2.0];
    }
    
    if([[resDic valueForKey:@"ok"] boolValue]){
        if([[resDic valueForKey:@"requestname"] isEqualToString:@"tupian"])
        {

//            if(_imageString){
//                [_imageString release];
//            }
//            _imageString = [[NSString alloc]initWithString:[resDic valueForKey:@"fileurl"]];
//            if(_soundData){
//                [self sendSound];
//            }else{
//                [self sendMessage];
//            }
//        }else{
//            if(_soundString){
//                [_soundString release];
//            }
//            _soundString = [[NSString alloc]initWithString:[resDic valueForKey:@"fileurl"]];
//            [FileManager removeItemAtPath:[AppComManager pathForMedia:@"yuyin.amr"] error:nil];
//            [self sendMessage];
        }
    }
}


//BanBu_Upload_My_Photos


-(void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    if(error)
    {
        [TKLoadingView showTkloadingAddedTo:targetViewController.view title:NSLocalizedString(@"uploadFailNotice", nil) activityAnimated:NO duration:2.0];
        if(_waveImageModified)
        {
            _waveImageModified(YES);
        }
        return;
    }
    else if([AppComManager respondsDic:resDic isFunctionData:BanBu_Set_User_Avatar]){
        if([[resDic valueForKey:@"ok"] boolValue])
        {

            [AppComManager getBanBuData:BanBu_Set_Spaceset_Byuser par:[NSDictionary dictionaryWithObjectsAndKeys:[resDic valueForKey:@"largepic"],@"valueof",KEY_WAVE_PICTURE,@"keyof",nil] delegate:self];
            [TKLoadingView showTkloadingAddedTo:targetViewController.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
            
        }else{
            if(_waveImageModified)
            {
                _waveImageModified(NO);
            }
        }
    }
    else if([AppComManager respondsDic:resDic isFunctionData:BanBu_Set_Spaceset_Byuser])
    {
        [TKLoadingView showTkloadingAddedTo:targetViewController.navigationController.view title:NSLocalizedString(@"modifySucceed", nil) activityAnimated:NO duration:1];
        if(_waveImageModified)
        {
            _waveImageModified(YES);
        }
    }

}
@end
